package com.example.aigc_education.service.converter;
import com.alibaba.fastjson.JSON;
import com.example.aigc_education.domain.Paper;
import com.example.aigc_education.domain.dto.FlashCardDTO;
import com.example.aigc_education.domain.dto.QuizQuestionDTO;
import com.example.aigc_education.domain.vo.*;
import org.mapstruct.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper(componentModel = "spring")
public  interface PaperConverter {


    /**
     * Paper-->BriefFlashCardListVO
     */
    @Mappings(
            value = {
                    @Mapping(source = "paperId", target = "flashCardListId"),
                    @Mapping(source = "paperName", target = "flashCardListName"),
                    @Mapping(source = "questionNum", target = "flashCardNum"),
                    @Mapping(target = "answerStatus",ignore = true)
            }
    )
    BriefFlashCardListVO paperToBriefFlashCardListVO(Paper paper);

    List<BriefFlashCardListVO> papersToBriefFlashCardListVOs(List<Paper> papers);

    //调用完自动转化后，会自动调用本方法
    @AfterMapping
    default void getAnswerStatusMap(Paper paper, @MappingTarget BriefFlashCardListVO briefFlashCardListVO){
        String answerStatus = paper.getAnswerStatus();
        // 将字符串按逗号分割
        String[] parts = answerStatus.split(",");
        Map<String, Integer> answerStatusMap = new HashMap<>();
        // 假设parts[0]是对数，parts[1]是错数，parts[2]是未记得的数
        // 对数
        answerStatusMap.put("correct", Integer.parseInt(parts[0]));
        // 错数
        answerStatusMap.put("wrong", Integer.parseInt(parts[1]));
        // 不记得数
        answerStatusMap.put("forget", Integer.parseInt(parts[2]));
        briefFlashCardListVO.setAnswerStatus(answerStatusMap);
    }



    /**
     * FlashCardVO-->FlashCardDTO
     */
    FlashCardDTO flashCardVOToFlashCardDTO(FlashCardVO flashCardVO);

    List<FlashCardDTO> flashCardVOsToBriefFlashCardDTOs(List<FlashCardVO> flashCardVOS);



    /**
     * Paper-->EntireFlashCardListVO
     */
    @Mappings(
            value = {
                    @Mapping(source = "paperId", target = "flashCardListId"),
                    @Mapping(source = "paperName", target = "flashCardListName"),
                    @Mapping(source = "isComplete", target = "isComplete"),

            }
    )
    EntireFlashCardListVO paperToEntireFlashCardListVO(Paper paper);

    //调用完自动转化后，会自动调用本方法
    @AfterMapping
    default void geFLashCards(Paper paper, @MappingTarget EntireFlashCardListVO entireFlashCardListVO)
    {
        String questions = paper.getQuestions();
        List<FlashCardVO> flashCardVOS = JSON.parseArray(questions, FlashCardVO.class);
        for (FlashCardVO flashCardVO : flashCardVOS){
            if (flashCardVO.getAnswerStatus() == null) {
                Short answerStatus = 0;
                flashCardVO.setAnswerStatus(answerStatus);
            }
        }
        entireFlashCardListVO.setFlashCardVOS(flashCardVOS);
    }



    /**
     * Paper-->BriefQuizVO
     */
    @Mappings(
            value = {
                    @Mapping(source = "paperId", target = "quizId"),
                    @Mapping(source = "paperName", target = "quizName"),
                    @Mapping(source = "questionNum", target = "questionNum"),
                    @Mapping(target = "answerStatus",ignore = true)
            }
    )
    BriefQuizVO paperToBriefQuizVO(Paper paper);

    List<BriefQuizVO> papersToBriefQuizVOS(List<Paper> papers);

    //调用完自动转化后，会自动调用本方法
    @AfterMapping
    default void getQuizAnswerStatusMap(Paper paper, @MappingTarget BriefQuizVO briefQuizVO){
        String answerStatus = paper.getAnswerStatus();
        // 将字符串按逗号分割
        String[] parts = answerStatus.split(",");
        Map<String, Integer> answerStatusMap = new HashMap<>();
        // 假设parts[0]是对数，parts[1]是错数，parts[2]是未记得的数
        // 对数
        answerStatusMap.put("correct", Integer.parseInt(parts[0]));
        // 错数
        answerStatusMap.put("wrong", Integer.parseInt(parts[1]));
        briefQuizVO.setAnswerStatus(answerStatusMap);
    }




    /**
     * QuizQuestionVO-->QuizQuestionDTO
     */
    QuizQuestionVO quizQuestionDTOToQuizQuestionVO (QuizQuestionDTO quizQuestionDTO);

    List<QuizQuestionVO> quizQuestionDTOsToQuizQuestionVOs(List<QuizQuestionDTO> questionVOS);



    /**
     * Paper-->EntireQuizVO
     */
    @Mappings(
            value = {
                    @Mapping(source = "paperId", target = "quizId"),
                    @Mapping(source = "paperName", target = "quizName"),
                    @Mapping(source = "isComplete", target = "isComplete"),
            }
    )
    EntireQuizVO paperToEntireQuizVO(Paper paper);

    @AfterMapping
    default void getQuestions(Paper paper, @MappingTarget EntireQuizVO entireQuizVO)
    {
        String questions = paper.getQuestions();
        // 将问题JSON转换成集合
        List<QuizQuestionDTO> quizQuestionDTOS = JSON.parseArray(questions, QuizQuestionDTO.class);


        for (QuizQuestionDTO quizQuestionDTO : quizQuestionDTOS){
            //将问题里的选项JSON转为结构
            List<String> optionList = JSON.parseArray(quizQuestionDTO.getOptions(), String.class);
            //对questionList进行前端需要添加
            List<OptionVO> optionsVOS = new ArrayList<>();
            for (String s : optionList) {
                OptionVO optionVO = new OptionVO();
                optionVO.setLabel(s);
                if (quizQuestionDTO.getUserAnswer().contains(String.valueOf(s.charAt(0))) && !quizQuestionDTO.getQuestionType().equals("filling")){
                    optionVO.setSelected(true);
                }
                if (quizQuestionDTO.getUserAnswer().contains(s) && quizQuestionDTO.getQuestionType().equals("judge")){
                    optionVO.setSelected(true);
                }
                optionsVOS.add(optionVO);
            }
            quizQuestionDTO.setOptionList(optionsVOS);
            //如果为AnswerStatus空，自动赋值为未做
            if (quizQuestionDTO.getAnswerStatus() == null) {
                Short answerStatus = 0;
                quizQuestionDTO.setAnswerStatus(answerStatus);
            }
        }
        List<QuizQuestionVO> questionVOS = this.quizQuestionDTOsToQuizQuestionVOs(quizQuestionDTOS);
        entireQuizVO.setQuestionVOS(questionVOS);
    }
}
